/**
 * Created by python on 19-4-17.
 */
/**
 * Created by python on 19-4-17.
 */


var vm = new Vue({
    el: '#big',
    delimiters: ['[[', ']]'], // 修改vue模板符号，防止与django冲突
    data: {
        host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        avatar_url: sessionStorage.avatar_url || localStorage.avatar_url,
        token: sessionStorage.token || localStorage.token,
        nick_name: sessionStorage.nick_name || localStorage.nick_name,
        // // 页面中需要使用到的数据，键值对

        answers:[],
        quest_id:'',
        create_time:'',
        title:'',
        label:'',
        answer_count:'',
        new_time:'',
        answer:'',
        user:'',
        num:0,
        labels:[],

    },

    mounted: function () {
        label_id = this.get_query_string('label_id');
        axios.get(host + "/answers/labels/",{
            params:{
                label_id:label_id
            },

            // 向后端传递JWT token的方法
            headers: {
                // 后台接收的JWT对格式有要求,'JWT'开头,空一格拼接字符串
                'Authorization': 'JWT ' + this.token
            },
            responseType: 'json',
            // withCredentials: true
      })

        .then(function (response ) {
            // console.log(response)
           vm.answers = response.data;
            console.log(vm.answers)
        } )

          //获取标签的相关内容
          axios.get(host + "/answers/labelsdetail")

            .then(function (response ) {
                // console.log(response)
               vm.labels = response.data;
                console.log(vm.labels)
            } )



    },
    methods: {
        logout: function() {
            sessionStorage.clear();
            localStorage.clear();
            location.href = './person-loginsign.html';
        },
        // 获取url路径参数
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },

        // 请求数据
		list:function () {
			vm.num = 0,
            axios.get(host + '/answers/?ordering=-new_answer_time')
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

		hot:function () {
			vm.num = 1,
            axios.get(host + '/answers/?ordering=-answer_count')
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

		wait:function () {
			vm.num = 2,
            axios.get(host + '/answers/?answer_count=0')
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

        // 点击页数
        on_page: function (num) {
            if (num != this.page) {
                this.page = num;
                this.get_spits();
            }
        },


            // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        getDateDiff: function(Time){
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - Time;
	if(diffValue < 0){return;}
	var monthC =diffValue/month;
	var weekC =diffValue/(7*day);
	var dayC =diffValue/day;
	var hourC =diffValue/hour;
	var minC =diffValue/minute;
	if(monthC>=1){
		result="" + parseInt(monthC) + "月前";
	}
	else if(weekC>=1){
		result="" + parseInt(weekC) + "周前";
	}
	else if(dayC>=1){
		result=""+ parseInt(dayC) +"天前";
	}
	else if(hourC>=1){
		result=""+ parseInt(hourC) +"小时前";
	}
	else if(minC>=1){
		result=""+ parseInt(minC) +"分钟前";
	}else
	result="刚刚";
	return result;
},

  shijianchuo:function (Time) {
      var b = new Date(Time)
      return b.getTime()
  }
    },




});